<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="设备ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="id">
              <a-input v-model="model.id" :maxLength="30" :disabled="deviceIdDisabled" placeholder="请输入设备ID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceType">
              <j-dict-select-tag type="list"  v-model="model.deviceType" dictCode="device_type" placeholder="请选择设备类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceName">
              <a-input v-model="model.deviceName" :maxLength="255" placeholder="请输入设备名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceNo">
              <a-input v-model="model.deviceNo" :maxLength="50" placeholder="请输入设备编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备地址码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceMac">
              <a-input v-model="model.deviceMac" :maxLength="255" placeholder="请输入设备地址码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.deviceType == 8">
            <a-form-model-item label="打卡设备用途" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="features">
              <j-dict-select-tag type="list" v-model="model.features" dictCode="device_features" placeholder="请选择打卡设备用途" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comments">
              <a-textarea v-model="model.comments" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入备注" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.comments?model.comments.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SkDeviceForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
            id: [
              { required: true, message: '请输入设备id!'},
           ],
           deviceType: [
              { required: true, message: '请选择设备类型!'},
           ],
           deviceName: [
              { required: true, message: '请输入设备名称!'},
           ],
           deviceNo: [
              { required: true, message: '请输入设备编号!'},
           ],
           features: [
              { required: true, message: '请选择打卡设备用途!'},
           ],
           deviceMac: [
              { required: true, message: '请输入设备地址码!'},
           ],
        },
        url: {
          add: "/management/skDevice/add",
          edit: "/management/skDevice/edit",
          queryById: "/management/skDevice/queryById"
        },
        status:1,
        deviceIdDisabled:false,
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      disabledId(){
        return this.model.id ?true:false
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (status) {
        this.status = status;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (this.model.id) {
          this.deviceIdDisabled = true;
        }
        this.visible = true;
      },
      submitForm () {
        console.log(this.model);
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.status){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.$emit("modelLoading",that.confirmLoading)
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>